﻿Namespace My

    ' I seguenti eventi sono disponibili per MyApplication:
    ' 
    ' Startup: generato all'avvio dell'applicazione, prima della creazione del form di avvio.
    ' Shutdown: generato dopo la chiusura di tutti i form dell'applicazione. L'evento non è generato se l'applicazione termina in modo anormale.
    ' UnhandledException: generato se l'applicazione rileva un'eccezione non gestita.
    ' StartupNextInstance: generato quando si avvia un'applicazione istanza singola e l'applicazione è già attiva. 
    ' NetworkAvailabilityChanged: generato quando la connessione di rete è connessa o disconnessa.
    Partial Friend Class MyApplication

        Private Sub EvalArguments()

            If My.Application.CommandLineArgs.Count = 2 Then

                Dim strNameProject As String = My.Application.CommandLineArgs.Item(0).ToString
                Dim intVariant As Launcher.ExecVariant = IIf(My.Application.CommandLineArgs.Item(1).ToString = "RELEASE", Launcher.ExecVariant.eExecVariant_RELEASE, Launcher.ExecVariant.eExecVariant_TRACE)

                ' Find project info using project name
                Dim oProjectInfo As ProjectInfo = Nothing
                For Each oProjectInfo In glistProjectsInfo
                    If oProjectInfo.strName = strNameProject Then
                        Exit For
                    End If
                Next
                If oProjectInfo Is Nothing Then
                    MsgBox("Project not found.", MsgBoxStyle.Critical, My.Application.Info.AssemblyName)
                    Return
                End If

                frmMain.txtConsole1.Text = ""
                Dim oLauncher As New Launcher(gVirtualMachine, oProjectInfo, intVariant)
                AddHandler oLauncher.OnReceiveDataToTTY0, AddressOf frmMain.OnReceivDataTTY0
                If oLauncher.LaunchUT() = False Then
                    MsgBox(oLauncher.strErrorMsg)
                Else
                    'MsgBox(String.Format("{0} executed", strNameProject), MsgBoxStyle.Information + MsgBoxStyle.SystemModal, My.Application.Info.AssemblyName)
                End If

                oProjectInfo = Nothing
                oLauncher = Nothing
            End If

        End Sub

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            ' Load settings
            Dim strMainDirectory As String = GetSetting(My.Application.Info.AssemblyName, "Settings", "Host_MainDirectory", "")
            gVirtualMachine.Load(My.Application.Info.AssemblyName, "Settings")

            ' Load projects info
            Call LoadListProjectInfo(strMainDirectory)

            ' Evaluate arguments and exec commands
            Call EvalArguments()

        End Sub

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            ' Evaluate arguments and exec commands
            Call EvalArguments()
        End Sub

    End Class


End Namespace

